﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;

namespace Portal.Systems.Render
{
    public class UserRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string Keyword = "";
                //string UserType = (string)WebEnvironments.Request(SiteId, "type");
                //if (string.IsNullOrEmpty(UserType)) UserType= "";
                string RoleId = "";
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientAdd()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.DrawAddForm(SiteId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtLoginName').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientUpdate(UserId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.DrawUpdateForm(SiteId, UserId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtLoginName').focus();\r\n" +
                   "    }\r\n" +

                  "    function ClientBlock(UserId,LockVal)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn khóa chưa?')==false)return;\r\n" +
                   "        RoleId = document.getElementById('txtSelectedRole').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.ServerSideBlock(SiteId, UserId, RoleId, Keyword,LockVal).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListUsers').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +


                   "    function ClientInputData(UserId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "    }\r\n" +


                   "    function ClientBack()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divActionForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientAddUsers()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        LoginName = document.getElementById('txtLoginName').value;\r\n" +
                   "        FullName = document.getElementById('txtFullName').value;\r\n" +

                   "        RoleId = document.getElementById('cbRoleId').options[document.getElementById('cbRoleId').selectedIndex].value;\r\n" +
                    //"        RoleId = document.getElementById('txtSelectedRole').value;\r\n" +

                   "        Password= document.getElementById('txtPassword').value;\r\n" +
                   "        RePassword= document.getElementById('txtRePassword').value;\r\n" +
                   "        Keyword= '';\r\n" +
                   "        if(document.getElementById('txtKeyword')!=null)\r\n" +
                   "        {\r\n" +
                   "            Keyword=document.getElementById('txtKeyword').value;\r\n" +
                   "        }\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.ServerSideAdd(SiteId, LoginName, FullName, RoleId, Password, RePassword, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListUsers').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListUsers').style.display='block';\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n" +

                   "    function NextPage(CurrentPageIndex)\r\n" +
                   "    {\r\n" +
                   "        ClientReading(CurrentPageIndex);\r\n" +
                   "    }\r\n" +

                   "    function ClientReading(CurrentPageIndex)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        RoleId = document.getElementById('txtSelectedRole').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.ServerSideReading(SiteId, RoleId, Keyword, CurrentPageIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListUsers').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientDelete(UserId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                   "        RoleId = document.getElementById('txtSelectedRole').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.ServerSideDelete(SiteId, UserId, RoleId, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListUsers').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientSaveUsers(UserId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        LoginName = document.getElementById('txtLoginName').value;\r\n" +
                   "        FullName = document.getElementById('txtFullName').value;\r\n" +
                   //"        RoleId = document.getElementById('txtSelectedRole').value;\r\n" +
                   "        RoleId = document.getElementById('cbRoleId').options[document.getElementById('cbRoleId').selectedIndex].value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.ServerSideSave(SiteId,UserId, LoginName, FullName, RoleId, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListUsers').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n" +

                   "    function ClientChangePassword(UserId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +


                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.DrawResetPasswordForm(SiteId, UserId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtPassword').focus();\r\n" +
                   "    }\r\n" +

                   "    function SaveChangePassword(AssetCode,LoginName)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        Password=document.getElementById('txtPassword').value;\r\n" +
                   "        RePassword=document.getElementById('txtRePassword').value;\r\n" +
                   "        UserType = document.getElementById('txtUserType').value;\r\n" +
                   "        Keyword = document.getElementById('txtKeyword').value;\r\n" +

                   "        AjaxOut = Portal.Systems.Render.UserRenderBll.ServerSideChangePassword(SiteId, AssetCode, LoginName, Password, RePassword, UserType, Keyword).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListUsers').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +


                   "    function ClientLoadRecord(Id)\r\n" +
                   "    {\r\n" +
                   "    alert(Id); \r\n" +
                   "        document.getElementById('txtSelectedRole').value=Id;\r\n" +
                   "        ClientReading();\r\n" +
                   "    }\r\n" +
                   "</script>\r\n" +
                   "<div id=divActionForm></div>\r\n" +
                    //"<input type=hidden id=txtUserType value=\"" + UserType + "\">\r\n" +
                   "<input type=hidden id=txtSelectedRole>\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\"" + ButtonIconRoot + "/Add.png\"><div>Tạo mới</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "    <div class=CssListUser>\r\n" +
                   "        <table>\r\n" +
                   "        <tr>\r\n" +
                   "            <td class=col1>\r\n" +
                   "                <div class=CssUlAccountType>\r\n" +
                   //DrawTreeView(SiteId).HtmlContent +
                    //ServerReadingAccountType(SiteId,UserType).HtmlContent +
                   "                </div>\r\n" +
                   "            </td>\r\n" +
                   "            <td class=col2>\r\n" +
                   "                   <div class=CssFilter>Từ khóa lọc:<input type=textbox class=textbox id=txtKeyword><input type=button class=button value=\"Lọc theo từ khóa\" onclick=\"javascript:ClientReading(0);\"></div>\r\n" +
                   "                   <div id=\"divListUsers\" class=tabcontent>\r\n" +
                                            ServerSideReading(SiteId, RoleId, Keyword, 0).HtmlContent +
                   "                   </div>\r\n" +
                   "            </td>\r\n" +
                   "        </tr>\r\n" +
                   "        </table>\r\n" +
                   "    </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawTreeView(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
                string Html =
                              "<script language=javascript>\r\n" +

                              "</script>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\"><a href=\"root\">Danh sách nhóm</a>\r\n" +
                              "     <ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" + AccountTypes[iIndex].AccountType + "\" target=\"_self\">" + AccountTypes[iIndex].Description + "</a>\r\n" +
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(string SiteId, string ParentAccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, ParentAccountType);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + AccountTypes[iIndex].AccountType + "\">" + AccountTypes[iIndex].Description + "</a>\r\n" +
                                BuildRole(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildRole(string SiteId, string AccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                RoleCls[] Roles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoles(SiteId, AccountType, null);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < Roles.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + Roles[iIndex].RoleId + "\"><span>" + Roles[iIndex].RoleName + " (" + Roles[iIndex].TotalUsers.ToString("#,##0") + ")</span></a></li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public static AjaxOut ServerReadingAccountType(string SiteId,string AccountType)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
        //        string ButtonUrl = WebConfig.GetIconRoot(OSiteParam);

        //        AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
        //        string AllAppendClass = "";
        //        if (string.IsNullOrEmpty(AccountType))
        //        {
        //            AllAppendClass = " class=active";
        //        }
        //        string Html =
        //            "<ul>\r\n"+
        //            " <li "+AllAppendClass+" onclick=\"javascript:window.open('AccountMan.aspx','_self');\">" +
        //            "   <div><img class=imageicon src=\""+ButtonUrl+"/AccountType/all.png\"></div>\r\n" +
        //            "   <div>Toàn bộ</div>\r\n" +
        //            "</li>\r\n";
        //        for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
        //        {
        //            string AppendClass = "";
        //            if (AccountTypes[iIndex].AccountType.Equals(AccountType))
        //            {
        //                AppendClass = " class=active";
        //            }
        //            Html +=
        //                " <li " + AppendClass + " onclick=\"javascript:window.open('AccountMan.aspx?type=" + AccountTypes[iIndex].AccountType + "','_self');\">" + 
        //                "   <div><img class=imageicon src=\""+ButtonUrl+"/AccountType/User.png\"></div>\r\n"+
        //                "   <div>"+AccountTypes[iIndex].Description + "</div>\r\n"+
        //                "</li>\r\n";
        //        }
        //        Html +=
        //           "    </ul>\r\n";

        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //    }

        //    return OAjaxOut;
        //}

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId, string RoleId, string Keyword, int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                UserCls[] Users = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingUsers(SiteId, RoleId, Keyword);
                if (Users.Length == 0)
                {
                    OAjaxOut.HtmlContent = "<div class=CssNoData>Chưa có tài khoản nào trong hệ thống</div>\r\n";
                    return OAjaxOut;
                }
                string Subject = "Danh sách tài khoản truy cập";
                if (!string.IsNullOrEmpty(RoleId))
                {
                    RoleCls ORole = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateRoleModel(SiteId, RoleId);
                    Subject = "Danh sách thuộc vị trí - " + ORole.RoleName;
                }
                string BlockButtonValue = "Khóa";
                ReturnPaging OReturnPaging = WebPaging.GetPaging(Users.Length, CurrentPageIndex, 20, 10, "NextPage");
                string Html =
                    //"        <h3>"+Subject+"</h3>\r\n" +
                   OReturnPaging.PagingText +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>STT</th>\r\n" +
                   "                    <th class=thCode>Mã</th>\r\n" +
                   "                    <th>Họ tên</th>\r\n" +
                   "                    <th>Trạng thái</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                    //"                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {

                    string Locked = "Hoạt động";
                    int LockedVal = 1;

                    if (Users[iIndex].Locked == 1)
                    {
                        Locked = "Đã khóa";
                        BlockButtonValue = "Kích hoạt";
                        LockedVal = 0;
                    }

                    string UpdateButton = "<input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + Users[iIndex].UserId + "');\">";
                    string BlockButton = "<input type=button class=button value=\"" + BlockButtonValue + "\" onclick=\"javascript:ClientBlock('" + Users[iIndex].UserId + "','" + LockedVal + "');\">";
                    string DeleteButton = "<input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + Users[iIndex].UserId + "');\">";
                    string PasswordButton = "<input type=button class=button value=\"Mật khẩu\" onclick=\"javascript:ClientChangePassword('" + Users[iIndex].UserId + "');\">";
                    string RoleButton = "<input type=button class=button value=\"Nhóm\" onclick=\"javascript:ClientChangePassword('" + Users[iIndex].UserId + "');\">";
                    if (Users[iIndex].LoginName.ToLower().Equals("admin"))
                    {
                        UpdateButton = "";
                        DeleteButton = "";
                    }

                    Html +=
                       "                <tr>\r\n" +
                       "                    <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "                    <td class=tdCode>" + Users[iIndex].LoginName + "</td>\r\n" +
                       "                    <td>" + Users[iIndex].FullName + "</td>\r\n" +
                       "                    <td>" + Locked + "</td>\r\n" +
                       "                    <td style=\"width:10px;\">" + BlockButton + "</td>\r\n" +
                       "                    <td style=\"width:10px;\">" + UpdateButton + "</td>\r\n" +
                       "                    <td style=\"width:10px;\">" + DeleteButton + "</td>\r\n" +
                       "                    <td style=\"width:10px;\">" + PasswordButton + "</td>\r\n" +
                        //"                    <td style=\"width:10px;\">" + RoleButton + "</td>\r\n" +
                       "                </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string RoleCombo = string.Empty;
                RoleCls[] Roles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoles(SiteId, string.Empty, string.Empty);
                RoleCombo += "<select id=\"cbRoleId\">";
                if (Roles != null)
                {
                    foreach (RoleCls _RoleCls in Roles)
                    {
                        RoleCombo += "<option value=\"" + _RoleCls.RoleId + "\">" + _RoleCls.RoleName + "</option>";
                    }
                }
                RoleCombo += "</select>";

                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddUsers();\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   " <div class=CssFormSaveUpdate>\r\n" +
                   "  <h3>Thêm mới tài khoản truy cập</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Nhóm người dùng:</td>\r\n" +
                   "        <td class=Col2>" + RoleCombo + "</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã truy cập:</td>\r\n" +
                   "        <td class=Col2><input id=txtLoginName value=\"\" class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Họ tên:</td>\r\n" +
                   "        <td class=Col2><input id=txtFullName class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mật khẩu:</td>\r\n" +
                   "        <td class=Col2><input id=txtPassword class=textbox type=password></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Nhập lại mật khẩu:</td>\r\n" +
                   "        <td class=Col2><input id=txtRePassword class=textbox type=password></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1></td>\r\n" +
                   "        <td class=Col2>Chú ý: Mật khẩu tối thiểu dài 6 kí tự và phải bao gồm cả chữ hoa chữ thường và chữ số</td>\r\n" +
                   "    </tr>\r\n" +
                    //"    <tr>\r\n" +
                    //"        <td class=Col1></td>\r\n" +
                    //"        <td class=Col2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:ClientAddUsers();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                    //"    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawResetPasswordForm(string SiteId, string UserId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                UserCls OUser = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateUserModel(SiteId, UserId);
                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:SaveChangePassword('" + OUser.AssetCode + "','" + OUser.LoginName + "');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n" +
                   "  <input id=txtChangePasswordUserId type=hidden value=\"" + OUser.UserId + "\">\r\n" +
                   "  <h3>Đổi lại mật khẩu</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã truy cập:</td>\r\n" +
                   "        <td class=Col2><span id=spanLoginName>" + OUser.LoginName + "</span></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Họ tên:</td>\r\n" +
                   "        <td class=Col2><span id=spanFullName>" + OUser.FullName + "</span></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mật khẩu:</td>\r\n" +
                   "        <td class=Col2><input id=txtPassword class=textbox type=password></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Nhập lại mật khẩu:</td>\r\n" +
                   "        <td class=Col2><input id=txtRePassword class=textbox type=password></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1></td>\r\n" +
                   "        <td class=Col2>Chú ý: Mật khẩu tối thiểu dài 6 kí tự và phải bao gồm cả chữ hoa chữ thường và chữ số</td>\r\n" +
                   "    </tr>\r\n" +
                    //"    <tr>\r\n" +
                    //"        <td class=Col1></td>\r\n" +
                    //"        <td class=Col2><input type=button class=button value=\"Đổi mật khẩu\" onclick=\"javascript:SaveChangePassword('" + OUser.UserId + "');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                    //"    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId, string UserId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                UserCls OUsers = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateUserModel(SiteId, UserId);

                string RoleCombo = string.Empty;
                RoleCls[] Roles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoles(SiteId, string.Empty, string.Empty);
                RoleCls[] RolesUrser = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRolesForUser(SiteId, UserId);

                if (RolesUrser.Length > 0) {
                    OUsers.RoleId = RolesUrser[0].RoleId;
                }

                RoleCombo += "<select id=\"cbRoleId\">";
                if (Roles != null)
                {
                    foreach (RoleCls _RoleCls in Roles)
                    {
                        if (_RoleCls.RoleId == OUsers.RoleId)
                        {
                            RoleCombo +=
                                "<option value=\"" + _RoleCls.RoleId + "\" selected>" + _RoleCls.RoleName + "</option>";
                        }
                        else {
                            RoleCombo +=
                            "<option value=\"" + _RoleCls.RoleId + "\">" + _RoleCls.RoleName + "</option>";
                        }
                    }
                }
                RoleCombo += "</select>";

                
                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSaveUsers('" + UserId + "');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n" +
                   "  <h3>Sửa tài khoản truy cập</h3>\r\n" +
                   "  <table>\r\n" +

                   "    <tr>\r\n" +
                   "        <td class=Col1>Nhóm người dùng:</td>\r\n" +
                   "        <td class=Col2>" + RoleCombo + "</td>\r\n" +
                   "    </tr>\r\n" +

                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã truy cập:</td>\r\n" +
                   "        <td class=Col2><input id=txtLoginName class=textbox type=textbox value=\"" + OUsers.LoginName + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tên truy cập:</td>\r\n" +
                   "        <td class=Col2><input id=txtFullName class=textbox type=textbox value=\"" + OUsers.FullName + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                    //"    <tr>\r\n" +
                    //"        <td class=Col1></td>\r\n" +
                    //"        <td class=Col2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:ClientSaveUsers('"+UserId+"');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                    //"    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(
            string SiteId,
            string LoginName,
            string FullName,
            string RoleId,
            string Password,
            string RePassword,
            string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(LoginName)) throw new Exception("Chưa nhập mã truy cập!");
                if (string.IsNullOrEmpty(FullName)) throw new Exception("Chưa nhập tên truy cập!");
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddUser(null, SiteId, AssetCode, null, LoginName, FullName, "", Password, RePassword, new string[] { RoleId }, false);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, RoleId, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(
            string SiteId,
            string UserId,
            string LoginName,
            string FullName,
            string RoleId,
            string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(LoginName)) throw new Exception("Chưa nhập mã truy cập!");
                if (string.IsNullOrEmpty(FullName)) throw new Exception("Chưa nhập tên truy cập!");

                RoleCls[] oldRolesUrser = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRolesForUser(SiteId, UserId);
                if (oldRolesUrser.Length > 0)
                {
                    for (int i = 0; i < oldRolesUrser.Length; i++)
                    {
                        SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).RemoveUserFromRole(null, SiteId, oldRolesUrser[i].RoleId, UserId);
                    }
                }

                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveUser(null, SiteId, UserId, LoginName, FullName,RoleId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, RoleId, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string UserId, string RoleId, string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeleteUser(null, SiteId, UserId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, RoleId, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideBlock(string SiteId, string UserId, string RoleId, string Keyword, int LockVal)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).BlockUser(null, SiteId, UserId, LockVal);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, RoleId, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideChangePassword(string SiteId, string AssetCode, string LoginName, string Password, string RePassword, string UserType, string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ChangePassword(SiteId, AssetCode, LoginName, Password, RePassword);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, UserType, Keyword, 0).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

    }
}
